from ckeditor.fields import RichTextField
from django.db import models
#定义博文分类模型
class BlogCategory(models.Model):
    name = models.CharField(max_length=50,verbose_name="博文类别")
    def __str__(self):
        return self.name
    class Meta:
        verbose_name = '博文类别'
        verbose_name_plural = verbose_name

#定义博文模型
class BlogArticle(models.Model):
    title = models.CharField(max_length=200,verbose_name="标题")
    content = RichTextField(verbose_name="内容")
    # 博客作者，使用外键与用户关联
    username = models.ForeignKey(to="users.BlogUser", on_delete=models.CASCADE,verbose_name="作者")
    created_at = models.DateTimeField(auto_now_add=True,blank=True, null=True,verbose_name="发布时间")
    updated_at = models.DateTimeField(auto_now=True,blank=True, null=True,verbose_name="更新时间")
    # 博客分类，使用外键与分类关联
    category = models.ForeignKey(BlogCategory,on_delete=models.CASCADE, null=True,verbose_name="类别")
    tags = models.CharField(max_length=100, blank=True, null=True,verbose_name="标签")
    cover_image = models.ImageField(upload_to='blog_covers/', null=True, blank=True,verbose_name="封面图")
    def __str__(self):
        return self.title

    class Meta:
        verbose_name = '博客文章'
        verbose_name_plural = verbose_name

#定义博文评论模型
class BlogComment(models.Model):
    # 关联到博文
    blog = models.ForeignKey(BlogArticle, on_delete=models.CASCADE, verbose_name="博文标题")
    commentor = models.ForeignKey(to="users.BlogUser", on_delete=models.CASCADE,verbose_name="评论者")
    content = RichTextField(verbose_name="内容")
    created_at = models.DateTimeField(auto_now_add=True,verbose_name="评论时间",null=True, blank=True,)
    def __str__(self):
        return f'Comment by {self.commentor} on {self.blog}'

    class Meta:
        verbose_name = '博文评论'
        verbose_name_plural = verbose_name

#博文阅读统计
class BlogStats(models.Model):
    blog = models.ForeignKey(BlogArticle, on_delete=models.CASCADE, verbose_name="博文标题")
    counts = models.IntegerField(verbose_name="阅读次数",null=True)

    class Meta:
        verbose_name = '阅读统计'
        verbose_name_plural = verbose_name

